#include "quickimage.h"
#include "windowsvc.h"
#include "quickdraw.h"
#include "quickshader.h"
#include "Box2D/Box2D.h"
#include "irrKlang.h"
#include "glfont.h"

CTextureInterface*	g_pTexInterface = NULL;
CShaderInterface*	g_pShaderInterface = NULL;
CCameraObject*		g_pGlobalCam = NULL;

int		g_iScrWidth;
int		g_iScrHeight;
int		g_testImg;
int		g_backgroundImage;

float	g_globalFOV = 75.0f;

vec3f	g_guyPos(0.0f, 0.0f, 0.0f);

bool	g_bVSYNC;
bool	g_bFullscreen;
bool	g_bIsRunning = true;
bool	g_bDebugMode = false;

GLuint	g_uiFontTex = -1;
GLFont* g_pGameFont = NULL;

static void initAssets()
{
	vec3f key(151 / 255.0f, 153 / 255.0f, 153 / 255.0f);
	g_testImg = g_pTexInterface->AddTexture("Media/Textures/stickman.tga", SAMPLE_TRILINEAR | SAMPLE_CLAMP | TEXTURE_FILTER_KEY, &key);
	g_backgroundImage = g_pTexInterface->AddTexture("Media/Textures/background.bmp", SAMPLE_TRILINEAR | SAMPLE_CLAMP);

	glGenTextures(1, &g_uiFontTex);

	g_pGameFont = new GLFont();
	g_pGameFont->Create("Media/Textures/Fonts/giddyup_bold.glf", g_uiFontTex);

	vec3 pos = {0.0f, 0.0f, 0.0f};
	vec3 look = {0.0f, 0.0f, -1.0f};
	vec3 up = {0.0f, 1.0f, 0.0f};
	g_pGlobalCam->SetCamera(pos, look, up);
	g_pGlobalCam->SetPerspective(g_globalFOV, g_iScrWidth / (float)g_iScrHeight, 0.1f, 500.0f);
}

static void queryKeys()
{
	if(IsKeyDown('q') || IsKeyDown('Q'))
		SendQuitMsg();

	// Left Arrow //
	if(IsKeyDown(37))
	{
		g_pGlobalCam->MoveLeft(0.5f);
		g_guyPos -= vec3f(0.5f, 0.0f, 0.0f);
	}

	// Right Arrow //
	if(IsKeyDown(39))
	{
		g_guyPos += vec3f(0.5f, 0.0f, 0.0f);
		g_pGlobalCam->MoveRight(0.5f);
	}
}

static void queryMouse()
{
	if(IsKeyDown(192))
		g_bDebugMode = !g_bDebugMode;

	if(g_bDebugMode)
		SetCursorVisibility(1);
	else
	{
		SetCursorVisibility(0);

		int winX, winY;
		winX = winY = 0;

		s_window_placement wp = GetWindowLocation();
		int dw = (wp.left + wp.right) / 2;
		int dh = (wp.top + wp.bottom) / 2;

		int mx, my;
		GetMousePosition(mx, my);
		SetMousePosition(dw, dh);
	}
}

static void renderBackground()
{
	float height = 500.0f * sinf(deg_2_rad(g_globalFOV / 2.0f));
	float width = height * (g_iScrWidth / (float)g_iScrHeight);


	g_pTexInterface->BindTexture(g_backgroundImage);

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-width, -height, -300.0f);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(width, -height, -300.0f);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(width, height, -300.0f);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-width, height, -300.0f);
	glEnd();

	g_pTexInterface->EvictTextures();
}

static void renderGuy()
{
	float height = 5.0f;
	float width = 2.0f;

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	g_pTexInterface->BindTexture(g_testImg);

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-width + g_guyPos.x, -height + g_guyPos.y, -15.0f);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(width + g_guyPos.x, -height + g_guyPos.y, -15.0f);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(width + g_guyPos.x, height + g_guyPos.y, -15.0f);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-width + g_guyPos.x, height + g_guyPos.y, -15.0f);
	glEnd();

	g_pTexInterface->EvictTextures();
	glDisable(GL_BLEND);
}

static void renderScene()
{
	glClearColor(1, 1, 1, 0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	renderBackground();
	g_pGameFont->Begin();
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	g_pGameFont->TextOut("This medium... it's fragile...", 0, 0, -20.0);
	glDisable(GL_BLEND);
	glActiveTexture(GL_TEXTURE0);
	glDisable(GL_TEXTURE_2D);
	renderGuy();
	
	glFlush();
}




int main()
{
	quickINI iniFile("Media/config.ini");
	g_iScrWidth = quickINI::Instance()->getValueAsInt("window_width");
	g_iScrHeight = quickINI::Instance()->getValueAsInt("window_height");
	g_bVSYNC = quickINI::Instance()->getValueAsInt("vsync");
	g_bFullscreen = quickINI::Instance()->getValueAsInt("fullscreen");

	CreateAppWindow("LD22", g_iScrWidth, g_iScrHeight, 32, g_bFullscreen);
	initExtensions();
	g_pTexInterface = new CTextureInterface;
	g_pGlobalCam = new CCameraObject;

	initAssets();

	if(g_bVSYNC)
		wglSwapIntervalEXT(1);


	while(g_bIsRunning)
	{
		if(QueryQuitMsg())
			g_bIsRunning = false;

		queryKeys();
		queryMouse();
		g_pGlobalCam->Update();
		renderScene();
		FlipBuffers();

		// Do stuff //
	}

	DestroyAppWindow();

	delete g_pTexInterface;
	delete g_pGlobalCam;
	delete g_pGameFont;

	return 0;
}